/*      */ package com.zimbra.cs.wiki;
/*      */ 
/*      */ import com.zimbra.common.account.Key.AccountBy;
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.common.util.L10nUtil;
/*      */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*      */ import com.zimbra.common.util.Log;
/*      */ import com.zimbra.common.util.ZimbraLog;
/*      */ import com.zimbra.cs.account.Account;
/*      */ import com.zimbra.cs.account.Provisioning;
/*      */ import com.zimbra.cs.index.SortBy;
/*      */ import com.zimbra.cs.mailbox.Document;
/*      */ import com.zimbra.cs.mailbox.Folder;
/*      */ import com.zimbra.cs.mailbox.MailItem;
/*      */ import com.zimbra.cs.mailbox.MailItem.Type;
/*      */ import com.zimbra.cs.mailbox.Mailbox;
/*      */ import com.zimbra.cs.mailbox.WikiItem;
/*      */ import com.zimbra.cs.service.UserServlet;
/*      */ import com.zimbra.cs.service.doc.DocServiceException;
/*      */ import java.io.IOException;
/*      */ import java.text.DateFormat;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Collections;
/*      */ import java.util.Date;
/*      */ import java.util.HashMap;
/*      */ import java.util.Iterator;
/*      */ import java.util.List;
/*      */ import java.util.Locale;
/*      */ import java.util.Map;
/*      */ import java.util.Set;
/*      */ import java.util.StringTokenizer;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class WikiTemplate
/*      */   implements Comparable<WikiTemplate>
/*      */ {
/*      */   private String mId;
/*      */   private long mModifiedTime;
/*      */   private boolean mParsed;
/*      */   private List<Token> mTokens;
/*      */   private String mTemplate;
/*      */   private static final int WIKI_FOLDER_ID = 12;
/*      */   
/*      */   public WikiTemplate(String item, String id, String key, String name)
/*      */   {
/*   63 */     this(item);
/*   64 */     StringBuilder buf = new StringBuilder();
/*   65 */     if (id != null) buf.append(id);
/*   66 */     buf.append(":");
/*   67 */     if (key != null) buf.append(key);
/*   68 */     buf.append(":");
/*   69 */     if (name != null) buf.append(name);
/*   70 */     this.mId = buf.toString();
/*      */   }
/*      */   
/*   73 */   public WikiTemplate(String item) { this.mTemplate = item;
/*   74 */     this.mTokens = new ArrayList();
/*   75 */     this.mModifiedTime = 0L;
/*   76 */     this.mId = "";
/*   77 */     touch();
/*      */   }
/*      */   
/*      */   public String getRaw() {
/*   81 */     return this.mTemplate;
/*      */   }
/*      */   
/*      */   public static WikiTemplate findTemplate(Context ctxt, String name) throws ServiceException
/*      */   {
/*   86 */     WikiPage page = WikiPage.findTemplate(ctxt.wctxt, ctxt.item.getAccount().getId(), name);
/*   87 */     if (page == null)
/*   88 */       page = WikiPage.missingPage(name);
/*   89 */     return page.getTemplate(ctxt.wctxt);
/*      */   }
/*      */   
/*      */   public String toString(WikiPage.WikiContext ctxt, MailItem item) throws ServiceException, IOException
/*      */   {
/*   94 */     return toString(new Context(ctxt, item, this));
/*      */   }
/*      */   
/*      */   public String toString(WikiPage.WikiContext ctxt, MailItem item, MailItem latestVersionItem) throws ServiceException, IOException
/*      */   {
/*   99 */     return toString(new Context(ctxt, item, this, latestVersionItem));
/*      */   }
/*      */   
/*      */   public String toString(Context ctxt) throws ServiceException, IOException {
/*  103 */     if (!this.mParsed) {
/*  104 */       parse();
/*      */     }
/*      */     
/*  107 */     StringBuffer buf = new StringBuffer();
/*  108 */     for (Token tok : this.mTokens) {
/*  109 */       ctxt.token = tok;
/*  110 */       buf.append(apply(ctxt));
/*      */     }
/*  112 */     touch();
/*  113 */     return buf.toString();
/*      */   }
/*      */   
/*      */   private List<Token> getTokens() {
/*  117 */     return this.mTokens;
/*      */   }
/*      */   
/*      */   public long getModifiedTime() {
/*  121 */     return this.mModifiedTime;
/*      */   }
/*      */   
/*      */   public void parse() {
/*  125 */     if (!this.mParsed)
/*  126 */       Token.parse(this.mTemplate, this.mTokens);
/*  127 */     this.mParsed = true;
/*      */   }
/*      */   
/*      */   public String getId() {
/*  131 */     return this.mId;
/*      */   }
/*      */   
/*      */   public int compareTo(WikiTemplate t)
/*      */   {
/*  136 */     return this.mId.compareTo(t.mId);
/*      */   }
/*      */   
/*      */   public void getInclusions(Context ctxt, List<WikiTemplate> inclusions) {
/*  140 */     parse();
/*  141 */     for (Token tok : this.mTokens)
/*  142 */       if (tok.getType() != WikiTemplate.Token.TokenType.TEXT)
/*      */       {
/*  144 */         Wiklet w = Wiklet.get(tok);
/*  145 */         WikiTemplate t = null;
/*  146 */         if (w != null) {
/*      */           try {
/*  148 */             ctxt.token = tok;
/*  149 */             t = w.findInclusion(ctxt);
/*      */           }
/*      */           catch (Exception e) {}
/*  152 */           if ((t != null) && (!inclusions.contains(t))) {
/*  153 */             inclusions.add(t);
/*  154 */             t.getInclusions(ctxt, inclusions);
/*      */           }
/*      */         }
/*      */       }
/*      */   }
/*      */   
/*      */   private String apply(Context ctxt) throws ServiceException, IOException {
/*  161 */     if ((ctxt.token.getType() == WikiTemplate.Token.TokenType.TEXT) || (ctxt.token.getType() == WikiTemplate.Token.TokenType.COMMENT))
/*      */     {
/*  163 */       return ctxt.token.getValue(); }
/*  164 */     Wiklet w = Wiklet.get(ctxt.token);
/*  165 */     if (w != null) {
/*  166 */       String ret = w.apply(ctxt);
/*  167 */       return ret;
/*      */     }
/*  169 */     return "";
/*      */   }
/*      */   
/*      */   private void touch() {
/*  173 */     this.mModifiedTime = System.currentTimeMillis();
/*      */   }
/*      */   
/*      */ 
/*      */   public static class Token
/*      */   {
/*      */     public static final String sWIKLETTAG = "wiklet";
/*      */     
/*      */     public static final String sCLASSATTR = "class";
/*      */     
/*      */     private TokenType mType;
/*      */     private String mVal;
/*      */     private String mData;
/*      */     private Map<String, String> mParams;
/*      */     
/*      */     public static enum TokenType
/*      */     {
/*  190 */       TEXT,  WIKLET,  WIKILINK,  COMMENT;
/*      */       
/*      */       private TokenType() {} }
/*  193 */     public static void parse(String str, List<Token> tokens) throws IllegalArgumentException { int len = str.length();
/*  194 */       for (int pos = 0; pos < len;) {
/*  195 */         Token tok = null;
/*  196 */         int padding = 2;
/*  197 */         int end = pos;
/*  198 */         int lastPos = len - 1;
/*  199 */         if (pos >= lastPos)
/*  200 */           return;
/*  201 */         if (str.startsWith("<!--", pos))
/*      */         {
/*  203 */           end = str.indexOf("-->", pos);
/*  204 */           if (end > 0) {
/*  205 */             end += 3;
/*  206 */             tok = new Token(str.substring(pos, end), TokenType.COMMENT);
/*      */           }
/*  208 */         } else if (str.startsWith("{{", pos)) {
/*  209 */           end = str.indexOf("}}", pos);
/*  210 */           if (end > 0) {
/*  211 */             tok = new Token(str.substring(pos + 2, end), TokenType.WIKLET);
/*  212 */             end += padding;
/*      */           }
/*  214 */         } else if (str.startsWith("[[", pos)) {
/*  215 */           end = str.indexOf("]]", pos);
/*  216 */           if (end > 0) {
/*  217 */             tok = new Token(str.substring(pos + 2, end), TokenType.WIKILINK);
/*  218 */             end += padding;
/*      */           }
/*  220 */         } else if (str.startsWith("<wiklet", pos)) {
/*  221 */           end = str.indexOf(">", pos);
/*  222 */           if (end > 0) {
/*  223 */             if (str.charAt(end - 1) == '/') {
/*  224 */               padding = 1;
/*      */             } else {
/*  226 */               int endSection = str.indexOf("</wiklet>", end);
/*  227 */               padding = endSection - end + 9;
/*      */             }
/*  229 */             tok = new Token(str.substring(pos + 1, end), TokenType.WIKLET);
/*  230 */             end += padding;
/*      */           }
/*      */         }
/*      */         
/*  234 */         if (tok == null) {
/*  235 */           end = pos + 1;
/*  236 */           while ((end < lastPos) && 
/*  237 */             (!str.startsWith("<!--", end)) && (!str.startsWith("{{", end)) && (!str.startsWith("[[", end)) && (!str.startsWith("<wiklet", end)))
/*      */           {
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  243 */             end++;
/*      */           }
/*  245 */           if (end == lastPos)
/*  246 */             end = len;
/*  247 */           if (end > pos) {
/*  248 */             tok = new Token(str.substring(pos, end), TokenType.TEXT);
/*      */           }
/*      */         }
/*  251 */         if (tok != null) {
/*  252 */           tokens.add(tok);
/*      */         }
/*  254 */         pos = end;
/*      */       }
/*      */     }
/*      */     
/*      */     public Token(String text, TokenType type) {
/*  259 */       this.mVal = text;
/*  260 */       this.mType = type;
/*      */     }
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */     public TokenType getType()
/*      */     {
/*  269 */       return this.mType;
/*      */     }
/*      */     
/*      */     public String getValue() {
/*  273 */       return this.mVal;
/*      */     }
/*      */     
/*      */     public String getData() {
/*  277 */       return this.mData;
/*      */     }
/*      */     
/*      */     public void setData(String str) {
/*  281 */       this.mData = str;
/*      */     }
/*      */     
/*      */ 
/*  285 */     public Map<String, String> parseParam() { return parseParam(this.mVal); }
/*      */     
/*  287 */     private static enum ParseState { K,  V,  VQ;
/*      */       private ParseState() {} }
/*  289 */     public Map<String, String> parseParam(String text) { if (this.mParams != null)
/*  290 */         return this.mParams;
/*  291 */       Map<String, String> map = new HashMap();
/*  292 */       ParseState state = ParseState.K;
/*  293 */       String key = null;
/*  294 */       boolean done = false;
/*  295 */       char c = '\000';
/*  296 */       int start = 0; for (int end = 0; !done; end++) {
/*  297 */         char cprev = c;
/*  298 */         if (end == text.length()) {
/*  299 */           c = ' ';
/*  300 */           done = true;
/*      */         }
/*      */         else {
/*  303 */           c = text.charAt(end); }
/*  304 */         if (state == ParseState.K) {
/*  305 */           if ((c == ' ') || (c == '=')) {
/*  306 */             key = text.substring(start, end);
/*  307 */             start = end + 1;
/*  308 */             if (c == ' ') {
/*  309 */               map.put(key, key);
/*  310 */             } else if (c == '=')
/*  311 */               state = ParseState.V;
/*      */           }
/*  313 */         } else if (state == ParseState.V) {
/*  314 */           if ((c == '"') || (c == '\'')) {
/*  315 */             start++;
/*  316 */             state = ParseState.VQ;
/*  317 */           } else if (c == ' ') {
/*  318 */             map.put(key, text.substring(start, end));
/*  319 */             start = end + 1;
/*  320 */             state = ParseState.K;
/*      */           }
/*  322 */         } else if ((state == ParseState.VQ) && 
/*  323 */           ((c == '"') || (c == '\'')) && (cprev != '\\')) {
/*  324 */           map.put(key, text.substring(start, end));
/*  325 */           start = end + 1;
/*  326 */           state = ParseState.K;
/*      */         }
/*      */       }
/*      */       
/*  330 */       this.mParams = map;
/*  331 */       return map;
/*      */     }
/*      */     
/*      */ 
/*  335 */     public String toString() { return "Token: type=" + this.mType + ", text=" + this.mVal; } }
/*      */   
/*      */   public static class Context { public WikiPage.WikiContext wctxt;
/*      */     public MailItem item;
/*      */     public WikiTemplate itemTemplate;
/*      */     
/*  341 */     public Context(Context copy) { this(copy.wctxt, copy.item, copy.itemTemplate);
/*  342 */       this.locale = copy.wctxt.locale;
/*      */     }
/*      */     
/*  345 */     public Context(WikiPage.WikiContext wc, MailItem it, WikiTemplate itt) { this(wc, it, itt, null);
/*  346 */       this.locale = wc.locale;
/*      */     }
/*      */     
/*  349 */     public Context(WikiPage.WikiContext wc, MailItem it, WikiTemplate itt, MailItem tit) { this.wctxt = wc;this.item = it;this.itemTemplate = itt;this.content = null;this.latestVersionItem = tit;
/*  350 */       this.locale = wc.locale;
/*      */     }
/*      */     
/*      */ 
/*      */     public WikiTemplate.Token token;
/*      */     
/*      */     public String content;
/*      */     
/*      */     public MailItem latestVersionItem;
/*      */     
/*      */     public Locale locale;
/*      */   }
/*      */   
/*      */ 
/*      */   public static abstract class Wiklet
/*      */   {
/*      */     public String reportError(String errorMsg)
/*      */     {
/*  368 */       String msg = "Error handling wiklet " + getName() + ": " + errorMsg;
/*  369 */       ZimbraLog.doc.error(msg);
/*  370 */       return msg;
/*      */     }
/*      */     
/*      */ 
/*      */     protected String handleTemplates(WikiTemplate.Context ctxt, List<MailItem> list, String bodyTemplate, String itemTemplate)
/*      */       throws ServiceException, IOException
/*      */     {
/*  377 */       StringBuffer buf = new StringBuffer();
/*  378 */       for (MailItem item : list) {
/*  379 */         WikiTemplate t = WikiTemplate.findTemplate(ctxt, itemTemplate);
/*  380 */         buf.append(t.toString(ctxt.wctxt, item));
/*      */       }
/*  382 */       WikiTemplate.Context newCtxt = new WikiTemplate.Context(ctxt);
/*  383 */       newCtxt.content = buf.toString();
/*  384 */       WikiTemplate body = WikiTemplate.findTemplate(ctxt, bodyTemplate);
/*      */       
/*  386 */       return body.toString(newCtxt);
/*      */     }
/*      */     
/*  389 */     public String toString() { return "Wiklet: " + getName(); }
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  394 */     private static Map<String, Wiklet> sWIKLETS = new HashMap();
/*  395 */     static { addWiklet(new WikiTemplate.TocWiklet());
/*  396 */       addWiklet(new WikiTemplate.HistoryWiklet());
/*  397 */       addWiklet(new WikiTemplate.BreadcrumbsWiklet());
/*  398 */       addWiklet(new WikiTemplate.IconWiklet());
/*  399 */       addWiklet(new WikiTemplate.NameWiklet());
/*  400 */       addWiklet(new WikiTemplate.CreatorWiklet());
/*  401 */       addWiklet(new WikiTemplate.TagsWiklet());
/*  402 */       addWiklet(new WikiTemplate.ModifierWiklet());
/*  403 */       addWiklet(new WikiTemplate.CreateDateWiklet());
/*  404 */       addWiklet(new WikiTemplate.ModifyDateWiklet());
/*  405 */       addWiklet(new WikiTemplate.VersionWiklet());
/*  406 */       addWiklet(new WikiTemplate.ContentWiklet());
/*  407 */       addWiklet(new WikiTemplate.IncludeWiklet());
/*  408 */       addWiklet(new WikiTemplate.InlineWiklet());
/*  409 */       addWiklet(new WikiTemplate.WikilinkWiklet());
/*  410 */       addWiklet(new WikiTemplate.UrlWiklet());
/*  411 */       addWiklet(new WikiTemplate.FragmentWiklet());
/*  412 */       addWiklet(new WikiTemplate.MsgWiklet());
/*      */     }
/*      */     
/*      */     private static void addWiklet(Wiklet w) {
/*  416 */       sWIKLETS.put(w.getPattern(), w);
/*      */     }
/*      */     
/*      */     public static Wiklet get(WikiTemplate.Token tok) {
/*  420 */       String tokenStr = tok.getValue();
/*  421 */       Wiklet w; Wiklet w; if (tok.getType() == WikiTemplate.Token.TokenType.WIKILINK) {
/*  422 */         w = (Wiklet)sWIKLETS.get("WIKILINK");
/*      */       }
/*      */       else {
/*  425 */         int index = tokenStr.indexOf(' ');
/*  426 */         String firstTok; String firstTok; if (index != -1) {
/*  427 */           firstTok = tokenStr.substring(0, index);
/*      */         } else
/*  429 */           firstTok = tokenStr;
/*      */         Wiklet w;
/*  431 */         if (firstTok.equals("wiklet")) {
/*  432 */           Map<String, String> params = tok.parseParam();
/*  433 */           String cls = (String)params.get("class");
/*  434 */           if (cls == null)
/*      */           {
/*  436 */             return null; }
/*      */           Wiklet w;
/*  438 */           if (cls.equals("link")) {
/*  439 */             w = (Wiklet)sWIKLETS.get("WIKILINK");
/*      */           } else
/*  441 */             w = (Wiklet)sWIKLETS.get(cls.toUpperCase());
/*      */         } else {
/*  443 */           w = (Wiklet)sWIKLETS.get(firstTok);
/*      */         }
/*      */       }
/*  446 */       return w;
/*      */     }
/*      */     
/*      */ 
/*  450 */     public static Wiklet get(String name) { return (Wiklet)sWIKLETS.get(name); }
/*      */     
/*      */     public abstract String getName();
/*      */     
/*      */     public abstract String getPattern();
/*      */     
/*      */     public abstract String apply(WikiTemplate.Context paramContext) throws ServiceException, IOException;
/*      */     
/*      */     public abstract WikiTemplate findInclusion(WikiTemplate.Context paramContext) throws ServiceException, IOException; }
/*      */   
/*      */   public static class TocWiklet extends WikiTemplate.Wiklet { public static final String sFORMAT = "format";
/*      */     public static final String sBODYTEMPLATE = "bodyTemplate";
/*      */     public static final String sITEMTEMPLATE = "itemTemplate";
/*      */     public static final String sDEFAULTBODYTEMPLATE = "_TocBodyTemplate";
/*      */     public static final String sDEFAULTITEMTEMPLATE = "_TocItemTemplate";
/*  465 */     public static final String sSIMPLE = "simple"; public static final String sLIST = "list"; public static final String sTEMPLATE = "template"; public static final String[][] sTAGS = { { "zmwiki-tocList", "zmwiki-tocSimple" }, { "ul", "span" }, { "li", "span" } };
/*      */     
/*      */     public static final int sTAGLIST = 0;
/*      */     
/*      */     public static final int sTAGSIMPLE = 1;
/*      */     
/*      */     public static final int sCLASS = 0;
/*      */     
/*      */     public static final int sOUTER = 1;
/*      */     
/*      */     public static final int sINNER = 2;
/*      */     
/*      */ 
/*      */     public String getName()
/*      */     {
/*  480 */       return "Table of contents";
/*      */     }
/*      */     
/*  483 */     public String getPattern() { return "TOC"; }
/*      */     
/*      */ 
/*  486 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     private String createLink(String folder, String name) {
/*  489 */       StringBuffer buf = new StringBuffer();
/*  490 */       buf.append("<a href=\"");
/*  491 */       buf.append(folder + name);
/*  492 */       buf.append("\">");
/*  493 */       buf.append(name);
/*  494 */       buf.append("</a>");
/*  495 */       return buf.toString();
/*      */     }
/*      */     
/*      */     public String generateList(WikiTemplate.Context ctxt, int style) throws ServiceException {
/*  499 */       String prefix = "";
/*  500 */       Folder folder; if ((ctxt.item instanceof Folder)) {
/*  501 */         Folder folder = (Folder)ctxt.item;
/*  502 */         prefix = folder.getName() + "/";
/*      */       } else {
/*  504 */         folder = ctxt.item.getMailbox().getFolderById(ctxt.wctxt.octxt, ctxt.item.getFolderId()); }
/*  505 */       StringBuffer buf = new StringBuffer();
/*  506 */       buf.append("<");
/*  507 */       buf.append(sTAGS[1][style]);
/*  508 */       buf.append(" class='");
/*  509 */       buf.append(sTAGS[0][style]);
/*  510 */       buf.append("'>");
/*  511 */       List<Folder> subfolders = folder.getSubfolders(ctxt.wctxt.octxt);
/*  512 */       for (Folder f : subfolders) {
/*  513 */         buf.append("<");
/*  514 */         buf.append(sTAGS[2][style]);
/*  515 */         buf.append(" class='zmwiki-pageLink'>");
/*  516 */         buf.append(createLink(prefix, f.getName() + "/"));
/*  517 */         buf.append("</");
/*  518 */         buf.append(sTAGS[2][style]);
/*  519 */         buf.append(">");
/*      */       }
/*  521 */       Mailbox mbox = ctxt.item.getMailbox();
/*  522 */       for (Document doc : mbox.getDocumentList(ctxt.wctxt.octxt, folder.getId(), SortBy.NAME_NATURAL_ORDER_ASC)) {
/*  523 */         buf.append("<");
/*  524 */         buf.append(sTAGS[2][style]);
/*  525 */         buf.append(" class='zmwiki-pageLink'>");
/*  526 */         buf.append(createLink(prefix, doc.getName()));
/*  527 */         buf.append("</");
/*  528 */         buf.append(sTAGS[2][style]);
/*  529 */         buf.append(">");
/*      */       }
/*      */       
/*  532 */       buf.append("</");
/*  533 */       buf.append(sTAGS[1][style]);
/*  534 */       buf.append(">");
/*  535 */       return buf.toString();
/*      */     }
/*      */     
/*  538 */     public String applyTemplates(WikiTemplate.Context ctxt, Map<String, String> params) throws ServiceException, IOException { List<MailItem> list = new ArrayList();
/*      */       Folder folder;
/*  540 */       Folder folder; if ((ctxt.item instanceof Folder)) {
/*  541 */         folder = (Folder)ctxt.item;
/*      */       } else
/*  543 */         folder = ctxt.item.getMailbox().getFolderById(ctxt.wctxt.octxt, ctxt.item.getFolderId());
/*  544 */       list.addAll(folder.getSubfolders(ctxt.wctxt.octxt));
/*      */       
/*  546 */       Mailbox mbox = ctxt.item.getMailbox();
/*  547 */       MailItem.Type type = folder.getDefaultView();
/*  548 */       if (ctxt.wctxt.view == null) {
/*  549 */         list.addAll(mbox.getItemList(ctxt.wctxt.octxt, type, folder.getId(), SortBy.NAME_NATURAL_ORDER_ASC));
/*      */       } else {
/*  551 */         list.addAll(mbox.getItemList(ctxt.wctxt.octxt, MailItem.Type.of(ctxt.wctxt.view), folder.getId(), SortBy.NAME_NATURAL_ORDER_ASC));
/*      */       }
/*      */       
/*      */ 
/*  555 */       String bt = (String)params.get("bodyTemplate");
/*  556 */       String it = (String)params.get("itemTemplate");
/*  557 */       if (bt == null)
/*  558 */         bt = "_TocBodyTemplate";
/*  559 */       if (it == null)
/*  560 */         it = "_TocItemTemplate";
/*  561 */       return handleTemplates(ctxt, list, bt, it);
/*      */     }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException, IOException {
/*  565 */       Map<String, String> params = ctxt.token.parseParam();
/*  566 */       String format = (String)params.get("format");
/*  567 */       if (format == null) {
/*  568 */         format = "list";
/*      */       }
/*  570 */       if (format.equals("template")) {
/*  571 */         return applyTemplates(ctxt, params);
/*      */       }
/*      */       
/*  574 */       return generateList(ctxt, format.equals("simple") ? 1 : 0);
/*      */     }
/*      */   }
/*      */   
/*      */   public static class HistoryWiklet extends WikiTemplate.Wiklet
/*      */   {
/*      */     public static final String sFORMAT = "format";
/*      */     public static final String sBODYTEMPLATE = "bodyTemplate";
/*      */     public static final String sITEMTEMPLATE = "itemTemplate";
/*      */     public static final String sDEFAULTBODYTEMPLATE = "_TocVersionBodyTemplate";
/*      */     public static final String sDEFAULTITEMTEMPLATE = "_TocVersionItemTemplate";
/*      */     
/*      */     public String getName() {
/*  587 */       return "Version";
/*      */     }
/*      */     
/*  590 */     public String getPattern() { return "HISTORY"; }
/*      */     
/*      */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) {
/*  593 */       return null;
/*      */     }
/*      */     
/*      */     public String applyTemplates(WikiTemplate.Context ctxt, Map<String, String> params) throws ServiceException, IOException {
/*  597 */       Mailbox mbox = ctxt.item.getMailbox();
/*  598 */       List<MailItem> list = mbox.getAllRevisions(ctxt.wctxt.octxt, ctxt.item.getId(), ctxt.item.getType());
/*      */       
/*  600 */       Collections.reverse(list);
/*      */       
/*  602 */       String bt = (String)params.get("bodyTemplate");
/*  603 */       String it = (String)params.get("itemTemplate");
/*  604 */       if (bt == null)
/*  605 */         bt = "_TocVersionBodyTemplate";
/*  606 */       if (it == null)
/*  607 */         it = "_TocVersionItemTemplate";
/*  608 */       return handleTemplates(ctxt, list, bt, it);
/*      */     }
/*      */     
/*      */ 
/*      */     protected String handleTemplates(WikiTemplate.Context ctxt, List<MailItem> list, String bodyTemplate, String itemTemplate)
/*      */       throws ServiceException, IOException
/*      */     {
/*  615 */       StringBuffer buf = new StringBuffer();
/*  616 */       WikiTemplate t = WikiTemplate.findTemplate(ctxt, itemTemplate);
/*      */       
/*  618 */       buf.append(t.toString(ctxt.wctxt, ctxt.item, ctxt.item));
/*  619 */       for (MailItem item : list) {
/*  620 */         buf.append(t.toString(ctxt.wctxt, item, ctxt.item));
/*      */       }
/*  622 */       WikiTemplate.Context newCtxt = new WikiTemplate.Context(ctxt);
/*  623 */       newCtxt.content = buf.toString();
/*  624 */       WikiTemplate body = WikiTemplate.findTemplate(newCtxt, bodyTemplate);
/*      */       
/*  626 */       return body.toString(newCtxt);
/*      */     }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException, IOException
/*      */     {
/*  631 */       Map<String, String> params = ctxt.token.parseParam();
/*  632 */       return applyTemplates(ctxt, params);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */   public static class BreadcrumbsWiklet
/*      */     extends WikiTemplate.Wiklet
/*      */   {
/*      */     public static final String sPAGE = "page";
/*      */     public static final String sFORMAT = "format";
/*      */     public static final String sBODYTEMPLATE = "bodyTemplate";
/*      */     public static final String sITEMTEMPLATE = "itemTemplate";
/*      */     public static final String sSEPARATOR = "separator";
/*      */     public static final String sSIMPLE = "simple";
/*      */     public static final String sTEMPLATE = "template";
/*      */     public static final String sDEFAULTBODYTEMPLATE = "_PathBodyTemplate";
/*      */     public static final String sDEFAULTITEMTEMPLATE = "_PathItemTemplate";
/*      */     
/*      */     public String getName()
/*      */     {
/*  652 */       return "Breadcrumbs";
/*      */     }
/*      */     
/*  655 */     public String getPattern() { return "PATH"; }
/*      */     
/*      */ 
/*  658 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     private Folder getFolder(WikiTemplate.Context ctxt, MailItem item) throws ServiceException {
/*  661 */       Mailbox mbox = item.getMailbox();
/*  662 */       return mbox.getFolderById(ctxt.wctxt.octxt, item.getFolderId());
/*      */     }
/*      */     
/*  665 */     private List<MailItem> getBreadcrumbs(WikiTemplate.Context ctxt) { List<MailItem> list = new ArrayList();
/*      */       try {
/*  667 */         Folder f = getFolder(ctxt, ctxt.item);
/*  668 */         while (f.getId() != 1) {
/*  669 */           list.add(0, f);
/*  670 */           f = getFolder(ctxt, f);
/*      */         }
/*      */       }
/*      */       catch (ServiceException se) {}
/*      */       
/*      */ 
/*  676 */       return list;
/*      */     }
/*      */     
/*  679 */     public String apply(WikiTemplate.Context ctxt) throws ServiceException, IOException { List<MailItem> list = getBreadcrumbs(ctxt);
/*  680 */       Map<String, String> params = ctxt.token.parseParam();
/*  681 */       String format = (String)params.get("format");
/*  682 */       if ((format == null) || (format.equals("simple"))) {
/*  683 */         StringBuffer buf = new StringBuffer();
/*  684 */         buf.append("<span class='zmwiki-breadcrumbsSimple'>");
/*  685 */         StringBuffer path = new StringBuffer();
/*  686 */         path.append("/");
/*  687 */         for (MailItem item : list) {
/*  688 */           String name = item.getName();
/*  689 */           if (item.getId() == 12) {
/*  690 */             WikiTemplate.MsgWiklet msgWiklet = (WikiTemplate.MsgWiklet)WikiTemplate.Wiklet.get("MSG");
/*  691 */             String msgText = msgWiklet.getMessage(name, ctxt);
/*  692 */             if ((msgText != null) && (!msgText.equals(""))) {
/*  693 */               name = msgText;
/*      */             }
/*      */           }
/*  696 */           path.append(name);
/*  697 */           buf.append("<span class='zmwiki-pageLink'>");
/*  698 */           buf.append("[[").append(name).append("][").append(path).append("]]");
/*  699 */           buf.append("</span>");
/*  700 */           path.append("/");
/*      */         }
/*  702 */         buf.append("</span>");
/*  703 */         return new WikiTemplate(buf.toString()).toString(ctxt); }
/*  704 */       if (format.equals("template")) {
/*  705 */         String bt = (String)params.get("bodyTemplate");
/*  706 */         String it = (String)params.get("itemTemplate");
/*  707 */         if (bt == null)
/*  708 */           bt = "_PathBodyTemplate";
/*  709 */         if (it == null)
/*  710 */           it = "_PathItemTemplate";
/*  711 */         return handleTemplates(ctxt, list, bt, it);
/*      */       }
/*  713 */       return reportError("format " + format + " not recognized");
/*      */     }
/*      */   }
/*      */   
/*      */   public static class IconWiklet extends WikiTemplate.Wiklet {
/*      */     public String getName() {
/*  719 */       return "Icon";
/*      */     }
/*      */     
/*  722 */     public String getPattern() { return "ICON"; }
/*      */     
/*      */ 
/*  725 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  728 */       if ((ctxt.item instanceof Document)) {
/*  729 */         if ((ctxt.item instanceof WikiItem)) {
/*  730 */           return "<div class='ImgPage'></div>";
/*      */         }
/*  732 */         return "<div class='ImgAttachment'></div>";
/*      */       }
/*      */       
/*  735 */       if (ctxt.item.getFolderId() == 1)
/*  736 */         return "<div class='ImgNotebook'></div>";
/*  737 */       return "<div class='ImgSection'></div>";
/*      */     }
/*      */   }
/*      */   
/*      */   public static class NameWiklet extends WikiTemplate.Wiklet {
/*  742 */     public String getName() { return "Name"; }
/*      */     
/*      */     public String getPattern() {
/*  745 */       return "NAME";
/*      */     }
/*      */     
/*  748 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  751 */       String mText = ctxt.item.getName();
/*  752 */       if (ctxt.item.getId() == 12) {
/*  753 */         WikiTemplate.MsgWiklet msgWiklet = (WikiTemplate.MsgWiklet)WikiTemplate.Wiklet.get("MSG");
/*  754 */         String msgText = msgWiklet.getMessage(mText, ctxt);
/*  755 */         if ((msgText != null) && (!msgText.equals(""))) {
/*  756 */           mText = msgText;
/*      */         }
/*      */       }
/*  759 */       mText = mText.replaceAll("<", "&lt;");
/*  760 */       mText = mText.replaceAll(">", "&gt;");
/*  761 */       return mText;
/*      */     }
/*      */   }
/*      */   
/*      */   public static class FragmentWiklet extends WikiTemplate.Wiklet {
/*  766 */     public String getName() { return "Fragment"; }
/*      */     
/*      */     public String getPattern() {
/*  769 */       return "FRAGMENT";
/*      */     }
/*      */     
/*  772 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  775 */       if (!(ctxt.item instanceof Document))
/*  776 */         return "";
/*  777 */       Document doc = (Document)ctxt.item;
/*  778 */       return doc.getFragment();
/*      */     }
/*      */   }
/*      */   
/*      */   public static class CreatorWiklet extends WikiTemplate.Wiklet {
/*  783 */     public String getName() { return "Creator"; }
/*      */     
/*      */     public String getPattern() {
/*  786 */       return "CREATOR";
/*      */     }
/*      */     
/*  789 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException {
/*  792 */       if ((ctxt.item instanceof Folder))
/*      */       {
/*  794 */         return ctxt.item.getMailbox().getAccount().getName(); }
/*  795 */       if ((ctxt.item instanceof Document)) {
/*  796 */         Document doc = (Document)ctxt.item.getMailbox().getItemRevision(ctxt.wctxt.octxt, ctxt.item.getId(), ctxt.item.getType(), 1);
/*  797 */         if (doc != null) {
/*  798 */           return doc.getCreator();
/*      */         }
/*      */       }
/*  801 */       return "";
/*      */     }
/*      */   }
/*      */   
/*      */   public static class TagsWiklet extends WikiTemplate.Wiklet {
/*      */     public String getName() {
/*  807 */       return "Tags";
/*      */     }
/*      */     
/*  810 */     public String getPattern() { return "TAGS"; }
/*      */     
/*      */ 
/*  813 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException {
/*  816 */       if ((ctxt.item instanceof Folder)) {
/*  817 */         return "";
/*      */       }
/*  819 */       if ((ctxt.item instanceof Document)) {
/*  820 */         Document doc = (Document)ctxt.item;
/*  821 */         String[] tags = doc.getTags();
/*  822 */         StringBuffer names = new StringBuffer();
/*  823 */         int count = 0;
/*  824 */         int size = tags.length;
/*  825 */         if (size > 0) {
/*  826 */           names.append("<span class='zmwiki-tagsTitle'>Tags: </span>");
/*      */         }
/*  828 */         for (String tagName : tags) {
/*  829 */           count++;
/*  830 */           names.append("<span class='zmwiki-tags'>");
/*  831 */           names.append(tagName + (size == count ? " " : ", "));
/*  832 */           names.append(" </span>");
/*      */         }
/*  834 */         return names.toString();
/*      */       }
/*      */       
/*  837 */       return "";
/*      */     }
/*      */   }
/*      */   
/*      */   public static class ModifierWiklet extends WikiTemplate.Wiklet {
/*      */     public String getName() {
/*  843 */       return "Modifier";
/*      */     }
/*      */     
/*  846 */     public String getPattern() { return "MODIFIER"; }
/*      */     
/*      */ 
/*  849 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  852 */       if (!(ctxt.item instanceof Document))
/*  853 */         return "";
/*  854 */       Document doc = (Document)ctxt.item;
/*  855 */       return doc.getCreator();
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */   public static abstract class DateTimeWiklet
/*      */     extends WikiTemplate.Wiklet
/*      */   {
/*      */     private static final String sFORMAT = "format";
/*      */     
/*      */     private static final String sSHORTDATE = "shortdate";
/*      */     
/*      */     private static final String sMEDIUMDATE = "mediumdate";
/*      */     
/*      */     private static final String sLONGDATE = "longdate";
/*      */     private static final String sFULLDATE = "fulldate";
/*      */     private static final String sSHORTTIME = "shorttime";
/*      */     private static final String sMEDIUMTIME = "mediumtime";
/*      */     private static final String sLONGTIME = "longtime";
/*      */     private static final String sFULLTIME = "fulltime";
/*      */     private static final String sSHORTDATETIME = "shortdateandtime";
/*      */     private static final String sMEDIUMDATETIME = "mediumdateandtime";
/*      */     private static final String sLONGDATETIME = "longdateandtime";
/*      */     private static final String sFULLDATETIME = "fulldateandtime";
/*      */     protected static Map<String, DateFormat> sFORMATS;
/*  880 */     protected static Map<Locale, Map<String, DateFormat>> sLOCALEMAPS = new HashMap();
/*  881 */     static { sFORMATS = new HashMap();
/*      */       
/*  883 */       sFORMATS.put("shortdate", DateFormat.getDateInstance(3));
/*  884 */       sFORMATS.put("mediumdate", DateFormat.getDateInstance(2));
/*  885 */       sFORMATS.put("longdate", DateFormat.getDateInstance(1));
/*  886 */       sFORMATS.put("fulldate", DateFormat.getDateInstance(0));
/*      */       
/*  888 */       sFORMATS.put("shorttime", DateFormat.getTimeInstance(3));
/*  889 */       sFORMATS.put("mediumtime", DateFormat.getTimeInstance(2));
/*  890 */       sFORMATS.put("longtime", DateFormat.getTimeInstance(1));
/*  891 */       sFORMATS.put("fulltime", DateFormat.getTimeInstance(0));
/*      */       
/*  893 */       sFORMATS.put("shortdateandtime", DateFormat.getDateTimeInstance(3, 3));
/*  894 */       sFORMATS.put("mediumdateandtime", DateFormat.getDateTimeInstance(2, 2));
/*  895 */       sFORMATS.put("longdateandtime", DateFormat.getDateTimeInstance(1, 1));
/*  896 */       sFORMATS.put("fulldateandtime", DateFormat.getDateTimeInstance(0, 0));
/*      */     }
/*      */     
/*  899 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */ 
/*      */     protected DateFormat getDateFormat(Locale locale, String dateTimeStyle)
/*      */     {
/*  904 */       Map<String, DateFormat> dateFormat = (Map)sLOCALEMAPS.get(locale);
/*      */       
/*  906 */       if (dateFormat == null) {
/*  907 */         dateFormat = new HashMap();
/*  908 */         dateFormat.put("shortdate", DateFormat.getDateInstance(3, locale));
/*  909 */         dateFormat.put("mediumdate", DateFormat.getDateInstance(2, locale));
/*  910 */         dateFormat.put("longdate", DateFormat.getDateInstance(1, locale));
/*  911 */         dateFormat.put("fulldate", DateFormat.getDateInstance(0, locale));
/*      */         
/*  913 */         dateFormat.put("shorttime", DateFormat.getTimeInstance(3, locale));
/*  914 */         dateFormat.put("mediumtime", DateFormat.getTimeInstance(2, locale));
/*  915 */         dateFormat.put("longtime", DateFormat.getTimeInstance(1, locale));
/*  916 */         dateFormat.put("fulltime", DateFormat.getTimeInstance(0, locale));
/*      */         
/*  918 */         dateFormat.put("shortdateandtime", DateFormat.getDateTimeInstance(3, 3, locale));
/*  919 */         dateFormat.put("mediumdateandtime", DateFormat.getDateTimeInstance(2, 2, locale));
/*  920 */         dateFormat.put("longdateandtime", DateFormat.getDateTimeInstance(1, 1, locale));
/*  921 */         dateFormat.put("fulldateandtime", DateFormat.getDateTimeInstance(0, 0, locale));
/*  922 */         sLOCALEMAPS.put(locale, dateFormat);
/*  923 */         return (DateFormat)dateFormat.get(dateTimeStyle);
/*      */       }
/*  925 */       return (DateFormat)dateFormat.get(dateTimeStyle);
/*      */     }
/*      */     
/*      */     /* Error */
/*      */     protected String formatDate(WikiTemplate.Context ctxt, Date date)
/*      */     {
/*      */       // Byte code:
/*      */       //   0: aload_1
/*      */       //   1: getfield 24	com/zimbra/cs/wiki/WikiTemplate$Context:locale	Ljava/util/Locale;
/*      */       //   4: astore_3
/*      */       //   5: aload_1
/*      */       //   6: getfield 25	com/zimbra/cs/wiki/WikiTemplate$Context:token	Lcom/zimbra/cs/wiki/WikiTemplate$Token;
/*      */       //   9: invokevirtual 26	com/zimbra/cs/wiki/WikiTemplate$Token:parseParam	()Ljava/util/Map;
/*      */       //   12: astore 4
/*      */       //   14: aload 4
/*      */       //   16: ldc 27
/*      */       //   18: invokeinterface 3 2 0
/*      */       //   23: checkcast 28	java/lang/String
/*      */       //   26: astore 5
/*      */       //   28: aload 5
/*      */       //   30: ifnull +16 -> 46
/*      */       //   33: getstatic 29	com/zimbra/cs/wiki/WikiTemplate$DateTimeWiklet:sFORMATS	Ljava/util/Map;
/*      */       //   36: aload 5
/*      */       //   38: invokeinterface 30 2 0
/*      */       //   43: ifne +7 -> 50
/*      */       //   46: ldc 18
/*      */       //   48: astore 5
/*      */       //   50: aconst_null
/*      */       //   51: astore 6
/*      */       //   53: aload_3
/*      */       //   54: ifnonnull +21 -> 75
/*      */       //   57: getstatic 29	com/zimbra/cs/wiki/WikiTemplate$DateTimeWiklet:sFORMATS	Ljava/util/Map;
/*      */       //   60: aload 5
/*      */       //   62: invokeinterface 3 2 0
/*      */       //   67: checkcast 23	java/text/DateFormat
/*      */       //   70: astore 6
/*      */       //   72: goto +12 -> 84
/*      */       //   75: aload_0
/*      */       //   76: aload_3
/*      */       //   77: aload 5
/*      */       //   79: invokevirtual 31	com/zimbra/cs/wiki/WikiTemplate$DateTimeWiklet:getDateFormat	(Ljava/util/Locale;Ljava/lang/String;)Ljava/text/DateFormat;
/*      */       //   82: astore 6
/*      */       //   84: aload 6
/*      */       //   86: dup
/*      */       //   87: astore 7
/*      */       //   89: monitorenter
/*      */       //   90: aload 6
/*      */       //   92: aload_2
/*      */       //   93: invokevirtual 32	java/text/DateFormat:format	(Ljava/util/Date;)Ljava/lang/String;
/*      */       //   96: aload 7
/*      */       //   98: monitorexit
/*      */       //   99: areturn
/*      */       //   100: astore 8
/*      */       //   102: aload 7
/*      */       //   104: monitorexit
/*      */       //   105: aload 8
/*      */       //   107: athrow
/*      */       // Line number table:
/*      */       //   Java source line #931	-> byte code offset #0
/*      */       //   Java source line #933	-> byte code offset #5
/*      */       //   Java source line #934	-> byte code offset #14
/*      */       //   Java source line #935	-> byte code offset #28
/*      */       //   Java source line #936	-> byte code offset #46
/*      */       //   Java source line #937	-> byte code offset #50
/*      */       //   Java source line #938	-> byte code offset #53
/*      */       //   Java source line #939	-> byte code offset #57
/*      */       //   Java source line #941	-> byte code offset #75
/*      */       //   Java source line #943	-> byte code offset #84
/*      */       //   Java source line #944	-> byte code offset #90
/*      */       //   Java source line #945	-> byte code offset #100
/*      */       // Local variable table:
/*      */       //   start	length	slot	name	signature
/*      */       //   0	108	0	this	DateTimeWiklet
/*      */       //   0	108	1	ctxt	WikiTemplate.Context
/*      */       //   0	108	2	date	Date
/*      */       //   4	73	3	locale	Locale
/*      */       //   12	3	4	params	Map<String, String>
/*      */       //   26	52	5	format	String
/*      */       //   51	40	6	formatter	DateFormat
/*      */       //   87	16	7	Ljava/lang/Object;	Object
/*      */       //   100	6	8	localObject1	Object
/*      */       // Exception table:
/*      */       //   from	to	target	type
/*      */       //   90	99	100	finally
/*      */       //   100	105	100	finally
/*      */     }
/*      */   }
/*      */   
/*      */   public static class CreateDateWiklet
/*      */     extends WikiTemplate.DateTimeWiklet
/*      */   {
/*      */     public String getName()
/*      */     {
/*  950 */       return "Create Date";
/*      */     }
/*      */     
/*  953 */     public String getPattern() { return "CREATEDATE"; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException {
/*  956 */       MailItem item = ctxt.item.getMailbox().getItemRevision(ctxt.wctxt.octxt, ctxt.item.getId(), ctxt.item.getType(), 1);
/*  957 */       Date createDate = new Date((item == null ? ctxt.item : item).getDate());
/*  958 */       return formatDate(ctxt, createDate);
/*      */     }
/*      */   }
/*      */   
/*      */   public static class ModifyDateWiklet extends WikiTemplate.DateTimeWiklet {
/*  963 */     public String getName() { return "Modified Date"; }
/*      */     
/*      */ 
/*  966 */     public String getPattern() { return "MODIFYDATE"; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  969 */       Date modifyDate = new Date(ctxt.item.getDate());
/*  970 */       return formatDate(ctxt, modifyDate);
/*      */     }
/*      */   }
/*      */   
/*      */   public static class VersionWiklet extends WikiTemplate.Wiklet {
/*  975 */     public String getName() { return "Version"; }
/*      */     
/*      */     public String getPattern() {
/*  978 */       return "VERSION";
/*      */     }
/*      */     
/*  981 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*  984 */       if (!(ctxt.item instanceof Document))
/*  985 */         return "1";
/*  986 */       Document doc = (Document)ctxt.item;
/*  987 */       return Integer.toString(doc.getVersion());
/*      */     }
/*      */   }
/*      */   
/*      */   public static class ContentWiklet extends WikiTemplate.Wiklet {
/*  992 */     public String getName() { return "Content"; }
/*      */     
/*      */ 
/*  995 */     public String getPattern() { return "CONTENT"; }
/*      */     
/*      */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) throws ServiceException {
/*  998 */       WikiItem wiki = (WikiItem)ctxt.item;
/*  999 */       return WikiTemplate.findTemplate(ctxt, wiki.getWikiWord());
/*      */     }
/*      */     
/* 1002 */     public String apply(WikiTemplate.Context ctxt) throws ServiceException, IOException { if (ctxt.content != null)
/* 1003 */         return ctxt.content;
/* 1004 */       if (!(ctxt.item instanceof WikiItem))
/* 1005 */         return "<!-- cotent wiklet on non-wiki item -->";
/* 1006 */       WikiTemplate template = ctxt.itemTemplate;
/* 1007 */       if (template == null) {
/* 1008 */         WikiItem wiki = (WikiItem)ctxt.item;
/* 1009 */         template = WikiTemplate.findTemplate(ctxt, wiki.getWikiWord());
/*      */       }
/* 1011 */       for (WikiTemplate.Token t : template.getTokens()) {
/* 1012 */         WikiTemplate.Wiklet w = WikiTemplate.Wiklet.get(t);
/* 1013 */         if ((w == null) || (w.getName().equals(getName())))
/* 1014 */           return template.getRaw();
/*      */       }
/* 1016 */       return template.toString(ctxt);
/*      */     }
/*      */   }
/*      */   
/*      */   public static class InlineWiklet extends WikiTemplate.IncludeWiklet {
/* 1021 */     public String getPattern() { return "INLINE"; }
/*      */   }
/*      */   
/*      */   public static class IncludeWiklet extends WikiTemplate.Wiklet {
/*      */     public static final String sPAGE = "page";
/*      */     
/*      */     public String getName() {
/* 1028 */       return "Include";
/*      */     }
/*      */     
/* 1031 */     public String getPattern() { return "INCLUDE"; }
/*      */     
/*      */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) throws ServiceException {
/* 1034 */       Map<String, String> params = ctxt.token.parseParam();
/* 1035 */       String page = (String)params.get("page");
/* 1036 */       if (page == null) {
/* 1037 */         page = (String)params.keySet().iterator().next();
/*      */       }
/* 1039 */       return WikiTemplate.findTemplate(ctxt, page);
/*      */     }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/* 1043 */       try { WikiTemplate template = findInclusion(ctxt);
/* 1044 */         return template.toString(ctxt);
/*      */       } catch (Exception e) {}
/* 1046 */       return "<!-- missing template " + ctxt.token + " -->";
/*      */     }
/*      */   }
/*      */   
/*      */   public static class WikilinkWiklet extends WikiTemplate.Wiklet {
/*      */     private static final String PAGENAME = "pagename";
/*      */     private static final String TEXT = "text";
/*      */     
/*      */     public String getName() {
/* 1055 */       return "Wikilink";
/*      */     }
/*      */     
/* 1058 */     public String getPattern() { return "WIKILINK"; }
/*      */     
/*      */ 
/* 1061 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) throws ServiceException, IOException { String link;
/*      */       String title;
/* 1065 */       if (ctxt.token.getType() == WikiTemplate.Token.TokenType.WIKILINK) {
/* 1066 */         String text = ctxt.token.getValue();
/* 1067 */         String title; String link = title = text;
/* 1068 */         int pos = text.indexOf('|');
/* 1069 */         if (pos != -1) {
/* 1070 */           link = text.substring(0, pos);
/* 1071 */           title = text.substring(pos + 1);
/*      */         } else {
/* 1073 */           pos = text.indexOf("][");
/* 1074 */           if (pos != -1) {
/* 1075 */             title = text.substring(0, pos);
/* 1076 */             link = text.substring(pos + 2);
/*      */           }
/*      */         }
/* 1079 */         if (text.startsWith("<wiklet")) {
/* 1080 */           WikiTemplate template = new WikiTemplate(link);
/* 1081 */           link = template.toString(ctxt);
/*      */         }
/*      */       } else {
/* 1084 */         Map<String, String> params = ctxt.token.parseParam();
/* 1085 */         link = (String)params.get("pagename");
/* 1086 */         title = (String)params.get("text");
/* 1087 */         if (title == null)
/* 1088 */           title = link;
/*      */       }
/*      */       try {
/* 1091 */         StringBuffer buf = new StringBuffer();
/* 1092 */         buf.append("<a href='");
/* 1093 */         buf.append(UserServlet.getRestUrl(ctxt.item.getAccount()) + link);
/* 1094 */         buf.append("'>").append(title).append("</a>");
/* 1095 */         return buf.toString();
/*      */       } catch (Exception e) {}
/* 1097 */       return "<!-- invalid wiki url " + link + " -->" + title;
/*      */     }
/*      */   }
/*      */   
/*      */   public static class UrlWiklet extends WikiTemplate.Wiklet {
/*      */     private static final String sLABEL = "label";
/*      */     private static final String sKEY = "key";
/*      */     private static final String sTYPE = "type";
/*      */     private static final String sVERSIONURL = "version";
/*      */     private static final String sHISTORYURL = "history";
/*      */     
/* 1108 */     public String getName() { return "Url"; }
/*      */     
/*      */     public String getPattern() {
/* 1111 */       return "URL";
/*      */     }
/*      */     
/* 1114 */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) { return null; }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/* 1117 */       if (ctxt.item == null)
/* 1118 */         return "<!-- cannot resolve item for url wiklet -->";
/* 1119 */       String title = ctxt.item.getName();
/* 1120 */       title = title.replaceAll("<", "&lt;");
/* 1121 */       title = title.replaceAll(">", "&gt;");
/*      */       try {
/* 1123 */         WikiTemplate.WikiUrl wurl = new WikiTemplate.WikiUrl(ctxt.item);
/* 1124 */         Map<String, String> params = ctxt.token.parseParam();
/* 1125 */         String type = (String)params.get("type");
/*      */         
/* 1127 */         StringBuffer buf = new StringBuffer();
/* 1128 */         String url = null;
/* 1129 */         if ((type != null) && (type.equals("version")) && (ctxt.latestVersionItem != null)) {
/* 1130 */           wurl = new WikiTemplate.WikiUrl(ctxt.latestVersionItem);
/* 1131 */           url = wurl.getFullUrl(ctxt.wctxt, ctxt.latestVersionItem.getMailbox().getAccountId()) + "?ver=" + ctxt.item.getVersion();
/* 1132 */         } else if ((type != null) && (type.equals("history"))) {
/* 1133 */           if ((ctxt.item instanceof Folder)) {
/* 1134 */             url = null;
/*      */           } else {
/* 1136 */             wurl = new WikiTemplate.WikiUrl(ctxt.item);
/* 1137 */             title = (String)params.get("label");
/* 1138 */             String msgKey = (String)params.get("key");
/* 1139 */             if (msgKey != null) {
/* 1140 */               WikiTemplate.MsgWiklet msgWiklet = (WikiTemplate.MsgWiklet)WikiTemplate.Wiklet.get("MSG");
/* 1141 */               title = msgWiklet.getMessage(msgKey, ctxt);
/*      */             }
/* 1143 */             if (title == null) {
/* 1144 */               title = "history";
/*      */             }
/* 1146 */             url = wurl.getFullUrl(ctxt.wctxt, ctxt.item.getMailbox().getAccountId()) + "?view=" + "history";
/*      */           }
/*      */         } else {
/* 1149 */           url = wurl.getFullUrl(ctxt.wctxt, ctxt.item.getMailbox().getAccountId());
/*      */           
/* 1151 */           if (ctxt.item.getType() == MailItem.Type.DOCUMENT) {
/* 1152 */             String contentType = ((Document)ctxt.item).getContentType();
/* 1153 */             if ((contentType != null) && ((contentType.indexOf("application/x-zimbra-doc") >= 0) || (contentType.indexOf("application/x-zimbra-slides") >= 0) || (contentType.indexOf("application/x-zimbra-xls") >= 0))) {
/* 1154 */               url = url + "?fmt=html&preview=1";
/*      */             }
/*      */           }
/*      */         }
/*      */         
/* 1159 */         if (url != null) {
/* 1160 */           if (ctxt.item.getId() == 12) {
/* 1161 */             WikiTemplate.MsgWiklet msgWiklet = (WikiTemplate.MsgWiklet)WikiTemplate.Wiklet.get("MSG");
/* 1162 */             String msgText = msgWiklet.getMessage(title, ctxt);
/* 1163 */             if ((msgText != null) && (!msgText.equals(""))) {
/* 1164 */               title = msgText;
/*      */             }
/*      */           }
/* 1167 */           buf.append("<a href='");
/* 1168 */           buf.append(url);
/* 1169 */           buf.append("'>").append(title).append("</a>");
/*      */         }
/* 1171 */         return buf.toString();
/*      */       } catch (Exception e) {}
/* 1173 */       return "<!-- cannot generate URL for item " + title + " -->" + title;
/*      */     }
/*      */   }
/*      */   
/*      */   public static class MsgWiklet extends WikiTemplate.Wiklet {
/*      */     private static final String sKEY = "key";
/*      */     
/*      */     public String getName() {
/* 1181 */       return "Msg";
/*      */     }
/*      */     
/* 1184 */     public String getPattern() { return "MSG"; }
/*      */     
/*      */     public WikiTemplate findInclusion(WikiTemplate.Context ctxt) {
/* 1187 */       return null;
/*      */     }
/*      */     
/*      */     public String getMessage(String key, WikiTemplate.Context ctxt) {
/* 1191 */       String mText = "";
/*      */       try {
/* 1193 */         Locale lc = ctxt.item.getMailbox().getAccount().getLocale();
/* 1194 */         L10nUtil.MsgKey msgKey = L10nUtil.MsgKey.valueOf(key);
/* 1195 */         if (msgKey != null) {
/* 1196 */           String msgText = L10nUtil.getMessage(msgKey, lc, new Object[0]);
/* 1197 */           if (msgText != null) {
/* 1198 */             mText = msgText;
/*      */           }
/*      */         }
/*      */       }
/*      */       catch (Exception e) {}
/* 1203 */       return mText;
/*      */     }
/*      */     
/*      */     public String apply(WikiTemplate.Context ctxt) {
/*      */       try {
/* 1208 */         Map<String, String> params = ctxt.token.parseParam();
/* 1209 */         String key = (String)params.get("key");
/*      */         
/* 1211 */         return getMessage(key, ctxt);
/*      */       }
/*      */       catch (Exception e) {}
/* 1214 */       return ""; } }
/*      */   
/*      */   static class WikiUrl { private Account mAccount;
/*      */     private String mPath;
/*      */     private int mId;
/*      */     
/* 1220 */     public WikiUrl(MailItem item) throws ServiceException { if ((item instanceof Folder))
/* 1221 */         this.mIsFolder = true;
/* 1222 */       this.mUrl = item.getName();
/* 1223 */       this.mId = item.getFolderId();
/* 1224 */       this.mPath = item.getPath();
/* 1225 */       this.mAccount = item.getAccount();
/* 1226 */       parse();
/*      */     }
/*      */     
/*      */ 
/*      */     private String mUrl;
/*      */     
/*      */     private String mFilename;
/*      */     private List<String> mTokens;
/*      */     private boolean mIsFolder;
/*      */     private void parse()
/*      */     {
/* 1237 */       this.mTokens = new ArrayList();
/* 1238 */       int begin = 0;int end = 0;
/* 1239 */       if (this.mUrl.startsWith("//")) {
/* 1240 */         begin = 2;
/* 1241 */         end = this.mUrl.indexOf('/', begin);
/* 1242 */         this.mTokens.add("//");
/* 1243 */         this.mTokens.add(this.mUrl.substring(begin, end));
/* 1244 */         begin = end;
/* 1245 */       } else if (this.mUrl.startsWith("/")) {
/* 1246 */         this.mTokens.add("/");
/*      */       } else {
/* 1248 */         if (this.mId == -1)
/* 1249 */           throw new IllegalArgumentException("not absolute url: " + this.mUrl);
/* 1250 */         this.mTokens.add(Integer.toString(this.mId));
/*      */       }
/* 1252 */       this.mTokens.add(this.mUrl.substring(begin));
/* 1253 */       begin = this.mUrl.lastIndexOf("/");
/* 1254 */       if (begin > 0) {
/* 1255 */         this.mFilename = this.mUrl.substring(begin + 1);
/*      */       } else
/* 1257 */         this.mFilename = this.mUrl;
/*      */     }
/*      */     
/* 1260 */     public String getFullUrl(WikiPage.WikiContext ctxt, String referenceAccount) throws ServiceException { if ((this.mUrl != null) && (this.mUrl.startsWith("http://")))
/* 1261 */         return this.mUrl;
/* 1262 */       Account ownerAccount = getOwnerAccount(referenceAccount);
/* 1263 */       return UserServlet.getRestUrl(ownerAccount) + getPath(ctxt, ownerAccount);
/*      */     }
/*      */     
/*      */     public Account getOwnerAccount(String referenceAccount) throws ServiceException {
/* 1267 */       return referenceAccount == null ? null : inAnotherMailbox() ? Provisioning.getInstance().get(Key.AccountBy.name, (String)this.mTokens.get(1)) : Provisioning.getInstance().get(Key.AccountBy.id, referenceAccount);
/*      */     }
/*      */     
/*      */     public String getPath(WikiPage.WikiContext ctxt, Account acct)
/*      */       throws ServiceException
/*      */     {
/* 1273 */       if ((!isAbsolute()) && (this.mId < 1)) {
/* 1274 */         throw DocServiceException.INVALID_PATH(this.mUrl);
/*      */       }
/*      */       
/* 1277 */       StringBuilder p = new StringBuilder();
/* 1278 */       if ((inAnotherMailbox()) || (acct == null))
/*      */       {
/* 1280 */         p.append((String)this.mTokens.get(2));
/* 1281 */       } else if (isAbsolute())
/*      */       {
/* 1283 */         p.append(this.mUrl);
/* 1284 */       } else if (acct.equals(this.mAccount)) {
/* 1285 */         p.append(this.mPath);
/*      */ 
/*      */       }
/*      */       else
/*      */       {
/*      */ 
/* 1291 */         throw DocServiceException.INVALID_PATH(this.mUrl);
/*      */       }
/* 1293 */       return normalizePath(p.toString());
/*      */     }
/*      */     
/* 1296 */     public String getFolderPath(WikiPage.WikiContext ctxt, String referenceAccount) throws ServiceException { Account acct = getOwnerAccount(referenceAccount);
/* 1297 */       String url = getPath(ctxt, acct);
/* 1298 */       int index = url.lastIndexOf('/');
/* 1299 */       if (index > 0) {
/* 1300 */         return url.substring(0, index);
/*      */       }
/* 1302 */       return "/";
/*      */     }
/*      */     
/*      */ 
/*      */     private String normalizePath(String path)
/*      */       throws ServiceException
/*      */     {
/* 1309 */       List<String> tokens = new ArrayList();
/* 1310 */       StringTokenizer tok = new StringTokenizer(path, "/");
/* 1311 */       while (tok.hasMoreElements()) {
/* 1312 */         String token = tok.nextToken();
/* 1313 */         if (!token.equals("."))
/*      */         {
/* 1315 */           if (token.equals("..")) {
/* 1316 */             if (tokens.isEmpty()) {
/* 1317 */               throw DocServiceException.INVALID_PATH(path);
/*      */             }
/* 1319 */             tokens.remove(tokens.size() - 1);
/*      */           } else {
/* 1321 */             tokens.add(token);
/*      */           } } }
/* 1323 */       if (tokens.isEmpty()) {
/* 1324 */         return "/";
/*      */       }
/* 1326 */       if (path.endsWith("/"))
/* 1327 */         tokens.add("");
/* 1328 */       StringBuilder newPath = new StringBuilder();
/* 1329 */       for (String token : tokens) {
/* 1330 */         newPath.append("/").append(urlEscape(token));
/*      */       }
/* 1332 */       if (this.mIsFolder)
/* 1333 */         newPath.append("/");
/* 1334 */       return newPath.toString();
/*      */     }
/*      */     
/*      */     private String urlEscape(String str)
/*      */     {
/* 1339 */       if ((str.indexOf(' ') == -1) && (str.indexOf('\'') == -1) && (str.indexOf('"') == -1) && (str.indexOf('#') == -1) && (str.indexOf('?') == -1))
/* 1340 */         return str;
/* 1341 */       StringBuilder buf = new StringBuilder();
/* 1342 */       for (char c : str.toCharArray()) {
/* 1343 */         if (c == ' ') {
/* 1344 */           buf.append("%20");
/* 1345 */         } else if (c == '"') {
/* 1346 */           buf.append("%22");
/* 1347 */         } else if (c == '\'') {
/* 1348 */           buf.append("%27");
/* 1349 */         } else if (c == '#') {
/* 1350 */           buf.append("%23");
/* 1351 */         } else if (c == '?')
/* 1352 */           buf.append("%3F"); else
/* 1353 */           buf.append(c);
/*      */       }
/* 1355 */       return buf.toString();
/*      */     }
/*      */     
/* 1358 */     public boolean isAbsolute() { return (this.mTokens != null) && (((String)this.mTokens.get(0)).startsWith("/")); }
/*      */     
/*      */     public boolean inAnotherMailbox()
/*      */     {
/* 1362 */       return (this.mTokens != null) && (((String)this.mTokens.get(0)).equals("//"));
/*      */     }
/*      */     
/*      */     public String getToken(int pos) {
/* 1366 */       return (String)this.mTokens.get(pos);
/*      */     }
/*      */     
/* 1369 */     public String getFilename() { return this.mFilename; }
/*      */     
/*      */     public String getUrl() {
/* 1372 */       return this.mUrl;
/*      */     }
/*      */     
/*      */     public String toString()
/*      */     {
/* 1377 */       return "wikiUrl: " + this.mUrl + " in folderId" + this.mId;
/*      */     }
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/wiki/WikiTemplate.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */